# 此程序建立在停用词完备的情况下,对各种情况标记 ；思想就是串的遍历，类似于kmp算法
import time

from pymongo import MongoClient

import funtion as fun
import mongo_make_news as make
import python_kmp_string as kmp

client = MongoClient('localhost',27017)
db = client.Unicom
collection = db.query2_sign

stopwords_dic = make.make_stopwords_dic()
# Keyword_dic = make.make_Keyword_dic() #待补充
# kefu_dic = make.make_kefu_dic() #待补充
kefu_dic = ['客服','火车票','中国移动','中国联通','中国电信','快递']
#去停函数

for i in collection.find():
    jieba_Question = i['jiebaSegment']
    jieba_Keyword = i['jiebaKeywords']
    jieba_Result =i['jiebaSearchResult']

    fool_Question = i['foolSegment']
    fool_Keyword = i['foolKeywords']
    fool_Result = i['foolSearchResult']

    i['reason'] = ''

    if jieba_Keyword == None:
        jieba_Keyword = ''
    if jieba_Result == None:
        jieba_Result = ''
    if fool_Keyword == None:
        fool_Keyword = ''
    if fool_Result == None:
        fool_Result =''

    jieba_Question_delete_stopword = fun.remove_stopword( jieba_Question, stopwords_dic ) # Question去停处理
    jieba_Result_delete_stopword = fun.remove_stopword( jieba_Result, stopwords_dic ) # Result去停处理

    fool_Question_delete_stopword = fun.remove_stopword( fool_Question, stopwords_dic ) # Question去停处理
    fool_Result_delete_stopword = fun.remove_stopword( fool_Result, stopwords_dic ) # Result去停处理


    

    if jieba_Question_delete_stopword != '' or fool_Question_delete_stopword != '':
        if fun.match(jieba_Question_delete_stopword,jieba_Keyword) >= 1 or fun.match(fool_Question_delete_stopword, jieba_Keyword) >= 1:

            for j in kefu_dic:
                if j in jieba_Keyword or j in fool_Keyword :
                    i['reason'] = 'k' # 客服
            if i['reason'] == 'k' :
                print(i['reason'])
                collection.save(i)
                continue

            if (jieba_Result != '' or fool_Result != '') and (jieba_Result != None or fool_Result != None):
                if fun.match(jieba_Question_delete_stopword,jieba_Keyword) >= 2 or fun.match(fool_Question_delete_stopword, jieba_Keyword) >= 1: # 相当程度2
                    i['reason'] = 's' # 成功
                else:
                    i['reason'] = 'a'   #algorithm，查询算法有问题 

        #     else:
        #         if 'Keyword在searchdb6中能搜索到' :
        #             i['reason'] = 'a'   #关键词正确且全，库中也有，但是程序没有跑出结果，算法需要改进 
        #         else:
        #             i['reason'] = 'w'   # 数据库中搜不到 

        # else:#关键词和去停的question不相当则说明关键词错误或者不全，可能既错误又不全 
        #     if  i['Keyword'] == '错误':   
        #         i['reason'] = 'm'   #此情况较为复杂，词典需要改进，不能出现错误的关键词 
        #     if  i['Keyword'] == '不全':   
        #         i['reason'] = 'c'   #需在词典中添加关键词，添加什么还需人工，因为可能是某个关键词不全，或者关键词不够 

    else:#去停之后为空说明有不是查询
        i['reason'] = 'd'   #delete，不是查询
    print(i['reason'])
    collection.save(i)
client.close()